<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="handleExport">导出</el-button>
      </template>
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <template slot-scope="{ row }" slot="menu">
        <!-- <el-button type="text" icon="el-icon-view" size="small" plain style="border: 0; background-color: transparent !important" @click.stop="handleView(row)">详情</el-button> -->
        <el-button type="text" icon="el-icon-s-promotion" size="small" plain style="border: 0; background-color: transparent !important" @click.stop="handleCreateView(row)">
          详情
        </el-button>
      </template>
    </avue-crud>
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="productMaterialDialog.visible"
      v-if="productMaterialDialog.visible"
      custom-class="productMaterialDialog"
      width="60%"
      top="7vh"
    >
      <template v-if="title == '原材料领料单详情'">
        <div class="common-modal-sub-title">
          <i class="el-icon-vertical-bar avue-group__icon"></i>
          <h1 class="avue-group__title">基本信息</h1>
        </div>
        <avue-form :option="baseOption" v-model="baseForm" ref="form"></avue-form>
        <div class="common-modal-sub-title">
          <i class="el-icon-vertical-bar avue-group__icon"></i>
          <h1 class="avue-group__title">物料明细</h1>
        </div>
      </template>
      <div class="header" v-if="title == '详情'">
        领料单号：{{ productMaterialDialog.outboundOrderNo }} 工单号：{{ productMaterialDialog.outboundOrderNo }} 产品编码：{{ productMaterialDialog.outboundOrderNo }} 状态：{{
          productMaterialDialog.outboundOrderNo
        }}
      </div>
      <avue-crud :option="productMaterialDialog.option" :table-loading="productMaterialDialog.loading" :data="productMaterialDialog.data"> </avue-crud>
      <div slot="footer" class="avue-dialog__footer" v-if="title == '原材料领料单详情'">
        <el-button size="small" icon="el-icon-circle-close" @click="productMaterialDialog.visible = false">取消</el-button>
        <!-- <el-button type="primary" size="small" icon="el-icon-s-promotion" @click="handleCreatePreMaterialList">生成备料单</el-button> -->
      </div>
    </el-dialog>
    <export-comp ref="exportComp" page-api-url="mes-warehouse/outbound-order/page-query" :option="option" :page="page" :query="exportParams" file-name="原材料领料单">
    </export-comp>
  </basic-container>
</template>

<script>
import { getList } from '@/api/warehouse/productMaterial';
import { mapGetters } from 'vuex';
import userTableConfig from '@/components/user-table-config';
import userConfig from '@/mixins/userConfig';
import exportComp from '@/components/export-comp';
export default {
  components: {
    userTableConfig,
    exportComp
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'Product_Material',
      form: {},
      baseForm: {},
      title: '详情',
      exportParams: {},
      loading: false,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      data: [],
      option: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchSpan: 6,
        searchMenuPosition: 'right',
        searchMenuSpan: 24,
        searchLabelWidth: 100,
        dialogClickModal: false,
        border: true,
        height: 'calc(100vh - 406px)',
        viewBtn: false,
        addBtn: false,
        selection: true,
        printBtn: false,
        editBtn: false,
        delBtn: false,
        index: false,
        columnBtn: false,
        menu: true,
        menuWidth: 200,
        column: [
          {
            label: '单据类型',
            prop: 'outboundOrderType',
            type: 'select',
            dicData: [
              {
                label: '用料清单',
                value: 'BOMFWO'
              },
              {
                label: '领料单',
                value: 'PRF'
              },
              {
                label: '补料单',
                value: 'PSF'
              }
            ],
            search: true,
            searchOrder: 1
          },
          {
            label: '待出库单号',
            prop: 'outboundOrderNo',
            search: true,
            searchOrder: 2
          },
          {
            label: '工单号',
            prop: 'workOrderNo',
            search: true,
            searchOrder: 3
          },
          {
            label: '产品编码',
            prop: 'productPartNo',
            search: true,
            searchOrder: 4
          },
          {
            label: '产品型号',
            prop: 'productModel',
            search: true,
            searchOrder: 5
          },
          {
            label: '部门',
            prop: '部门',
            search: true,
            searchOrder: 7
          },
          {
            label: '是否已备料',
            prop: '是否已备料',
            search: true,
            searchOrder: 8,
            type: 'select',
            dicData: [
              {
                label: '是',
                value: 1
              },
              {
                label: '否',
                value: 0
              }
            ]
          },
          {
            label: '状态',
            prop: 'status',
            type: 'select',
            width: 100,
            search: true,
            searchOrder: 6,
            dicData: [
              {
                label: '待出库',
                value: 1
              },
              {
                label: '已完成',
                value: 2
              },
              {
                label: '取消',
                value: 3
              },
              {
                label: '关闭',
                value: 4
              },
              {
                label: '部分出库',
                value: 5
              }
            ]
          }
        ]
      },
      productMaterialDialog: {
        data: [],
        outboundOrderNo: '',
        visible: false,
        loading: false,
        option: {
          tip: false,
          searchShow: true,
          searchSpan: 4,
          searchMenuSpan: 6,
          dialogClickModal: false,
          height: '400',
          border: true,
          addBtn: false,
          editBtn: false,
          viewBtn: false,
          delBtn: false,
          columnBtn: false,
          cancelBtn: false,
          saveBtn: false,
          selection: false,
          menu: false,
          menuWidth: 100,
          page: false,
          index: true,
          indexLabel: '序号',
          refreshBtn: false,
          expandBtn: false,
          column: [
            {
              label: '物料编码',
              prop: 'partNo'
            },
            {
              label: '物料名称',
              prop: 'partName'
            },
            {
              label: '物料型号',
              prop: 'partModel'
            },
            {
              label: '工段',
              prop: 'sectionName'
            },
            {
              label: '内部批次',
              prop: 'lotNo'
            },
            { label: '品牌', prop: 'partBrand' },
            {
              label: '需求数量',
              prop: 'requireQuantity'
            },
            {
              label: '已发数量',
              prop: 'actualQuantity'
            },
            {
              label: '待发数量',
              prop: 'dueoutQuantity'
            },
            {
              label: '超发数量',
              prop: '超发数量'
            },
            {
              label: '发料状态',
              prop: 'status',
              type: 'select',
              width: 100,
              dicData: [
                {
                  label: '待出库',
                  value: 1
                },
                {
                  label: '已完成',
                  value: 2
                },
                {
                  label: '取消',
                  value: 3
                },
                {
                  label: '关闭',
                  value: 4
                },
                {
                  label: '部分出库',
                  value: 5
                }
              ]
            }
          ]
        }
      },
      baseOption: {
        emptyBtn: false,
        submitBtn: false,
        detail: true,
        column: [
          { label: '领料单号', prop: 'outboundOrderNo', span: 12 },
          { label: '工单号', prop: 'workOrderNo', span: 12 },
          { label: '产品编码', prop: 'productPartNo', span: 12 },
          {
            label: '状态',
            prop: 'status',
            span: 12,
            type: 'select',
            dicData: [
              {
                label: '待出库',
                value: 1
              },
              {
                label: '已完成',
                value: 2
              },
              {
                label: '取消',
                value: 3
              },
              {
                label: '关闭',
                value: 4
              },
              {
                label: '部分出库',
                value: 5
              }
            ]
          }
        ]
      }
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        // viewBtn: this.vaildData(this.permission.finishWarehouseManage_view, false)
      };
    }
  },
  methods: {
    handleView(row) {
      this.title = '详情';
      this.productMaterialDialog.visible = true;
      this.productMaterialDialog.outboundOrderNo = row.outboundOrderNo;
      this.productMaterialDialog.data = row.outboundOrderDetailList;
    },
    handleExport() {
      this.$refs.exportComp.visible = true;
      this.exportParams = this.query;
    },
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    },
    beforeOpen(done, type) {
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query))
        .then((res) => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.selectionClear();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleCreateView(row) {
      this.title = '原材料领料单详情';
      this.productMaterialDialog.visible = true;
      this.baseForm = structuredClone(row);
      row.outboundOrderDetailList.forEach((ele) => {
        ele.dueoutQuantity = ele.requireQuantity - ele.actualQuantity;
      });
      this.productMaterialDialog.data = row.outboundOrderDetailList;
    },
    handleCreatePreMaterialList() {
      this.$message.warning('暂未实现');
      this.productMaterialDialog.visible = false;
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .productMaterialDialog {
  .el-dialog__body {
    padding: 0 20px 20px;
  }
  .avue-crud__menu {
    display: none;
  }
  .common-modal-sub-title {
    display: flex;
    align-items: center;
    font-size: 16px;
    font-weight: bold;
    margin: 10px 0;
  }
  .header {
    margin: 10px 0;
  }
}
</style>
